TalentSync

AI resume builder and ATS optimizer for job seekers. Tailored resumes, outreach, and interview prep from one profile.

Backend architecture

The FastAPI backend architecture for the TalentSync project.

Repository layout#

The backend is organized around a FastAPI application :

  • Application entrypoint and middleware registration
  • Core modules for configuration, dependency injection, logging, and exceptions
  • Route modules grouped by feature area
  • Service modules implementing business logic and integrations
  • Shared models and schemas

Building blocks#

  • FastAPI application with lifespan for startup/shutdown hooks
  • Middleware stack:
  • CORS
  • Request ID tracking via context variables
  • Request/response logging with payload formatting
  • Configuration management via Pydantic settings with environment-backed defaults
  • Dependency injection for settings, LLM instances, and per-request LLM overrides
  • Logging configuration with request ID propagation and structured formatting
  • Centralized exception types for consistent HTTP error responses
  • LLM factory supporting multiple providers with singletons and per-request overrides

How it fits together#

The system follows a layered architecture:

  • Presentation Layer: FastAPI routes define endpoints and accept/return Pydantic models
  • Dependency Injection Layer: Dependencies resolve settings, LLM instances, and per-request LLM
  • Service Layer: Business logic orchestrates processing, validation, and external integrations
  • External Integrations: LLM providers, file processing utilities, optional web retrieval

Routing and modular API versions#

  • v1 routes include LinkedIn, PostgreSQL, Tips, Cold Mail (file-based), Hiring Assistant (file-based), Resume Analysis (file-based), Resume Improvement, Resume Enrichment, Cover Letter, ATS Evaluation (file-based), Tailored Resume (file-based), Interview, and LLM Configuration.
  • v2 routes include Cold Mail (text-based), Hiring Assistant (text-based), Resume Analysis (text-based), Resume Improvement, Resume Enrichment, Cover Letter, ATS Evaluation (text-based), Tailored Resume (text-based), JD Editor, and Interview.
  • File-based endpoints accept multipart/form-data and process uploaded files; text-based endpoints accept JSON or form-encoded payloads and operate on preformatted text.

Dependency injection pattern#

  • Settings dependency resolves a cached Settings instance from environment variables.
  • LLM dependencies:
  • Singleton LLM for server defaults
  • Faster LLM singleton for lightweight tasks
  • Per-request LLM builder reads headers to override provider/model/key/base per request
  • Routes inject LLM via Depends(get_request_llm), enabling dynamic provider selection while falling back to server defaults.

Middleware stack#

  • CORS: Configured via settings with allow-all methods/headers and credentials support.
  • Request ID tracking:
  • Generates a UUID if none is provided
  • Binds request ID into a context variable for logging correlation
  • Propagates X-Request-ID header on responses
  • Request/response logging:
  • Logs request method, path, query, and sanitized payload
  • Streams response body, logs status and duration, and reconstructs Response

Logging architecture#

  • Structured logging with request ID filter applied to console and access handlers
  • Root and framework-specific loggers configured with appropriate levels
  • Access formatter integrates client address and request line with request ID
  • Request ID is bound/unbound per request lifecycle

Error handling strategies#

  • Centralized exception types for consistent HTTP responses (not found, bad request, unauthorized, forbidden, internal server error, service unavailable)
  • Route handlers catch validation errors and general exceptions, logging contextual details and raising appropriate HTTP exceptions
  • Service layer validates inputs, normalizes outputs, and raises HTTP exceptions on failures

Async/Await patterns and processing logic#

  • All route handlers are async and await file reads, form parsing, and service calls
  • Services orchestrate LLM calls and external integrations asynchronously
  • File-based endpoints stream uploads, process content, and clean up temporary files
  • Text-based endpoints accept preformatted text and perform analysis directly

Integration with external services#

  • LLM providers: OpenAI, Anthropic, Google Gemini, Ollama, OpenRouter, DeepSeek
  • Web content retrieval for job descriptions from links
  • File processing utilities for PDF, DOC, DOCX, TXT, MD

Dependencies#

  • Runtime dependencies include FastAPI, LangChain stack, PyMuPDF for PDF processing, SSE support, and HTTP client libraries
  • Dev dependencies include Uvicorn for local development
  • The application imports route modules centrally and registers them under v1 and v2 prefixes

Performance#

  • Use per-request LLM dependency to avoid blocking singletons for heavy tasks; reserve faster LLM for lightweight operations
  • Stream response bodies to avoid buffering entire responses in memory
  • Validate and sanitize payloads early to fail fast
  • Cache LLM singletons to reduce initialization overhead
  • Limit concurrent heavy LLM calls and batch operations where feasible
  • Keep file processing minimal and remove temporary files promptly

Troubleshooting#

  • LLM configuration issues:
  • Missing API keys or invalid provider/model lead to HTTP 503 during per-request LLM creation
  • Server defaults may return None if GOOGLE_API_KEY is not set for legacy provider
  • Validation errors:
  • Pydantic validation failures raise HTTP 400 with detailed messages
  • File processing errors:
  • Unsupported file types or processing failures raise HTTP 400
  • Web retrieval failures:
  • Link-based job descriptions failing to fetch raise HTTP 500
  • Logging:
  • Enable debug mode to increase verbosity and correlate logs via X-Request-ID